package org.jinyan.wechat.service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

import org.apache.http.HttpException;
import org.apache.log4j.Logger;
import org.jinyan.utils.CommonUtils;
import org.jinyan.utils.RemoteServiceUtil;

public class WechatToken {

	private String appId;
	private String appSecret;
	private String tokenUrl;
	private static Logger logger = Logger.getLogger(WechatToken.class);

	private String accessToken;
	private static WechatToken wechatToken = null;

	private WechatToken() {
		String path = CommonUtils.getPackagePath("/etc/servicesite.xml");

		try {
			String appIdA = (String) CommonUtils.readFromProperties(path,
					"AppId");
			String appSecretA = (String) CommonUtils.readFromProperties(path,
					"AppSecret");
			String tokenUrlA = (String) CommonUtils.readFromProperties(path,
					"TokenUrl");
			if (appIdA != null) {
				this.setAppId(appIdA);
			}
			if (appSecretA != null) {
				this.setAppSecret(appSecretA);
			}
			if (tokenUrlA != null) {
				this.setTokenUrl(tokenUrlA);
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	private static Timer timer; // 定时器

	private static int count = 0;
	private static int AllCount = 5000;

	public static WechatToken getInstance() {
		if (wechatToken == null || wechatToken.getAccessToken() == null) {
			wechatToken = new WechatToken();
			wechatToken.setAccessToken(getAT(wechatToken.getTokenUrl(),
					wechatToken.getAppId(), wechatToken.getAppSecret()));
		}
		if (timer == null) {
			timer = new Timer(true);
		}
		timer.schedule(new java.util.TimerTask() {
			public void run() {
				if (count++ > AllCount) {
					if (wechatToken == null) {
						wechatToken = new WechatToken();
					}
					wechatToken.setAccessToken(getAT(wechatToken.getTokenUrl(),
							wechatToken.getAppId(), wechatToken.getAppSecret()));
					AllCount = 5000;
					count = 0;
					logger.info("获取  AccessToken"
							+ wechatToken.getAccessToken());
				}
			}
		}, 0, 1000);

		return wechatToken;
	}

	public static String getAT(String url, String appId, String appSecret) {
		logger.info("获取 AccessToken  开始 url=" + url);
		String wechatToken = null;
		Map map = new HashMap<String, Object>();
		map.put("grant_type", "client_credential");
		map.put("appid", appId);
		map.put("secret", appSecret);
		Map result = null;

		try {
			result = RemoteServiceUtil.doHttpGetMethod(url, map);
		} catch (HttpException e) {
			AllCount = 30;
			logger.info("获取  AccessToken 失败 HttpException", e);
			e.printStackTrace();
		} catch (IOException e) {
			AllCount = 50;
			logger.info("获取  AccessToken 失败 IOException", e);
			e.printStackTrace();
		}
		if (result.containsKey("access_token")) {
			wechatToken = (String) result.get("access_token");
		}
		logger.info("获取  AccessToken=" + wechatToken);
		return wechatToken;
	}

	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getAppSecret() {
		return appSecret;
	}

	public void setAppSecret(String appSecret) {
		this.appSecret = appSecret;
	}

	public String getTokenUrl() {
		return tokenUrl;
	}

	public void setTokenUrl(String tokenUrl) {
		this.tokenUrl = tokenUrl;
	}

	public String getAccessToken() {
		return accessToken;
	}

	public void setAccessToken(String accessToken) {
		this.accessToken = accessToken;
	}

}
